package com.example.apple.printingroommanagementsystem;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by apple on 17/3/1.
 */
public class homePageoneFormat extends Fragment {
    Button cancellation_homepage,businessTypeList_homePage,listOfItems_homePage,listBusiness_homePage;
    SharedPreferences preferences;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.homepage_interface_one,container,false);
        cancellation_homepage = (Button)view.findViewById(R.id.cancellation_homepage);
        businessTypeList_homePage = (Button)view.findViewById(R.id.businessTypeList_homePage);
        listOfItems_homePage = (Button)view.findViewById(R.id.listOfItems_homePage);
        listBusiness_homePage = (Button)view.findViewById(R.id.listBusiness_homePage);
        cancellation_homepage.setOnClickListener(new onClickButtonResponse());
        businessTypeList_homePage.setOnClickListener(new onClickButtonResponse());
        listOfItems_homePage.setOnClickListener(new onClickButtonResponse());
        listBusiness_homePage.setOnClickListener(new onClickButtonResponse());
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        preferences =getActivity().getSharedPreferences("ure",getActivity().MODE_PRIVATE);
    }

    private class onClickButtonResponse implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.cancellation_homepage:
                    cancellationoHmepage();
                    break;
                case R.id.businessTypeList_homePage:
                    businessTypeListHomePage();
                    break;
                case R.id.listOfItems_homePage:
                    listOfItemsHomePage();

            }
        }
    }

    private void listOfItemsHomePage() {
        Intent intent = new Intent();
        intent.setClass(getContext(),ListOfItemsHomePageActivity.class);
        startActivity(intent);
    }

    void businessTypeListHomePage(){
        Intent intent = new Intent();
        intent.setClass(getContext(),BusinessTypeListHomePageActivity.class);
        startActivity(intent);
    }
    void cancellationoHmepage(){
        final AlertDialog dialog= new AlertDialog.Builder(getContext()).create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.cancellation_homepage);
        Button cancellation_Homepage_no,cancellation_Homepage_yes;
        cancellation_Homepage_no =(Button) window.findViewById(R.id.cancellation_Homepage_no);
        cancellation_Homepage_yes =(Button)window.findViewById(R.id.cancellation_Homepage_yes);
        cancellation_Homepage_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        cancellation_Homepage_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String token,ip;
                ip ="http://a.xl18z.cn/api/logout";
                token = preferences.getString("token","");
                GetRequest get = new GetRequest();
                get.doGet(ip,"?token="+token,new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        JSONObject js =(JSONObject) msg.obj;
                        try {
                            if (js.getString("errcode").equals("0")){
                                Toast.makeText(getContext(),"注销成功",Toast.LENGTH_SHORT).show();
                                // SystemClock.sleep(2000);
                                SharedPreferences.Editor editor = preferences.edit();
                                editor.putString("pwd","");
                                editor.commit();
                                Intent intent =new Intent();
                                intent.setClass(getContext(),MainActivity.class);
                                startActivity(intent);
                            }else {
                                // SystemClock.sleep(2000);
                                Toast.makeText(getContext(),"注销失败",Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }
}
